import com.alibaba.fastjson.JSONObject;
import org.apache.commons.codec.binary.Hex;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.web.client.RestTemplate;

import java.security.MessageDigest;
import java.util.Arrays;

/**
 * {
 *   "vendorShopId": "957299785",
 *   "date": "2021-07-28",
 *   "offset": 0,
 *   "limit": 5
 * }
 */
public class TestYqls {
    public static void main(String[] args) throws Exception {
        String appKey = "ebf8340f8ba7cb0327173299f689c9d8c664b1f4";
        String appSecret = "49c6be3b34eb165ccfa0bb167f6799fe6ed81cc2";
        String ts = String.valueOf(System.currentTimeMillis());
        String version = "2.0";
        JSONObject json = new JSONObject();

        json.put("date", "2021-07-28");
        json.put("vendorShopId", "957299785");
        json.put("offset", "0");
        json.put("limit", "5");
        String four = json.toJSONString();
        String[] params = new String[]{appSecret, "appKey"+appKey, "ts"+ts, "version"+version, four};
        Arrays.sort(params);
//        // 将5个参数字符串拼接成一个字符串进行sha1加密
        String clearText = params[0] + params[1] + params[2] + params[3] + params[4];
        String algorithm = "SHA-1";
        String sign = new String(Hex.encodeHex(MessageDigest.getInstance(algorithm).digest((clearText).getBytes()), true));
        System.out.println("sign:"+sign);

        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
        headers.setContentType(type);
        headers.add("Accept", MediaType.APPLICATION_JSON.toString());
        HttpEntity<String> formEntity = new HttpEntity<String>(json.toString(), headers);
        String url = "https://open-api.dianping.com/tuangou/coupon/queryLocalListByDate?version="+version+"&appKey="+appKey+"&ts="+ts+"&sign="+sign;
        String result = restTemplate.postForEntity(url, formEntity, String.class).getBody();
        System.out.println("result:"+result);
//
//        Mt mt= restTemplate.postForEntity(url, formEntity, Mt.class).getBody();
//        System.out.println("userPhoneTail:"+mt.getContent().getUserPhoneTail());










    }
}
